package com.cslg.ac.demo.controller.user;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.entity.user.person.PersonInfo;
import com.cslg.ac.demo.service.user.PersonInfoService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class PersonInfoController {

    @Autowired
    PersonInfoService personInfoService;
    @Value("${imageLength}")
    int imageLength;
    @Value("${nameLength}")
    int nameLength;
    @Value("${synopsisLength}")
    int synopsisLength;
    @Value("${cityLength}")
    int cityLength;
    @Value("${phoneLength}")
    int phoneLength;
    @Value("${lovevalue}")
    int lovevalue;


    @RequestMapping(value = "/person/getInfo",method = RequestMethod.POST)
    public void getInfo(@RequestParam int id,PrintWriter out, HttpSession session){
        PersonInfoPO personInfoPO;
        try {
            personInfoPO = personInfoService.getInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("person",personInfoPO);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);


    }

//    @RequestMapping(value = "/person/addLovevalue",method = RequestMethod.POST)
//    public void addLovevalue(@RequestParam Integer id,@RequestParam Integer count,PrintWriter out,HttpSession session){
//        if( !StringUtil.allNumber(id.toString(),count.toString()) || id == null || count == null || count < 0 || count > lovevalue ){
//            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
//            return;
//        }
//        int code = -1;
//        try {
//            code = personInfoService.addLovevalue(id,count);
//        } catch (Exception e) {
//            e.printStackTrace();
//            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
//        }
//        ResponseUtil.responseCode(out,code);
//    }

    @RequestMapping(value = "/person/simpleInfo",method = RequestMethod.POST)
    public void simpleInfo(PersonInfo personInfo, PrintWriter out, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(!personInfo.insertValid(nameLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        personInfo.setId(user.getId());
        personInfo.setPhone(user.getUserName());
        int tcode;
        try {
            tcode = personInfoService.simpleInfo(personInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,tcode);


    }


    @RequestMapping(value = "/person/changeInfo",method = RequestMethod.POST)
    public void changeInfo(PersonInfo personInfo,HttpSession session,PrintWriter out){
        User user = (User)session.getAttribute("user");
        personInfo.setId(user.getId());
        personInfo.setPhone(user.getUserName());
        if(!personInfo.updateValid(nameLength,cityLength,phoneLength,synopsisLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        PersonInfoPO personInfoPO = new PersonInfoPO();
        BeanUtils.copyProperties(personInfo,personInfoPO);
        int tcode = -1;
        try {
            tcode = personInfoService.changeInfo(personInfo);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
        }
        ResponseUtil.responseCode(out,tcode);


    }
}